import ExamApi from '@/repo/api//ExamAPI'
// import moment from "moment/moment";
import ExamAPI from "../../../repo/api/ExamAPI";
import db from "../../../repo/db/db"
import PlanAPI from "../../../repo/api/PlanAPI";

//获取试题详情
const getExamDetail = (examId, subjectId, type, callBack) => {
  if(type == 2){
    ExamApi.getExamDetailById(examId, null ,subjectId).then((result) => {

      let data = result.responseData
      let questions = data.questions

      questions.forEach((question) => {
        if (question.questions) {
          let mixId = question.id
          question.id = 'mix' + mixId
          question.isMix = true
        }
      })
      callBack(data)
    }).catch((error) => {

    })
  }else if(type == 1 || type == undefined){
    ExamApi.getExamDetailById(examId, subjectId).then((result) => {

      let data = result.responseData
      let questions = data.questions

      questions.forEach((question) => {
        if (question.questions) {
          let mixId = question.id
          question.id = 'mix' + mixId
          question.isMix = true
        }
      })
      callBack(data)
    }).catch((error) => {

    })
  }


}

//获取错误试题
const getErrorExamDetail = (examId, subjectId, callBack) => {

  ExamApi.getExamErrorById(examId, subjectId).then((result) => {

    let data = result.responseData
    let questions = data.questions || []

    questions.forEach((question) => {
      if (question.questions) {
        let mixId = question.id
        question.id = 'mix' + mixId
        question.isMix = true
      }
    })
    callBack(data)
  }).catch((error) => {

  })

}
//获取题目分析
const getExamAnalyze = (examResultId, callBack) => {

  ExamApi.getExamAnalyzeById(examResultId).then((result) => {

    let data = result.responseData
    let questions = data.questions

    questions.forEach((question) => {
      if (question.questions) {
        let mixId = question.id
        question.id = 'mix' + mixId
        question.isMix = true
      }
    })
    callBack(data)
  }).catch((error) => {

  })

}
//提交测试答案
const submitExam = (examId, subjectId, answers) => {

  let answerList = []

  for (let key in answers) {
    let item = answers[key];
    if (key.indexOf("mix") != -1) {
      //综合题
      for (let mixKey in item) {
        let mixItem = item[mixKey];
        let currentAnswer = mixItem.answer
        currentAnswer = currentAnswer.sort()

        answerList.push({'q': mixKey, 'r': JSON.stringify(currentAnswer)})
      }
    } else {
      //普通题
      let currentAnswer = item.answer
      currentAnswer = currentAnswer.sort()
      answerList.push({'q': key, 'r': JSON.stringify(currentAnswer)})
    }

  }

  return ExamApi.submitExam(examId, subjectId, answerList)

}

const submitExamPlan = (examId, subjectId, answers) => {

  let answerList = []

  for (let key in answers) {
    let item = answers[key];
    if (key.indexOf("mix") != -1) {
      //综合题
      for (let mixKey in item) {
        let mixItem = item[mixKey];
        let currentAnswer = mixItem.answer
        currentAnswer = currentAnswer.sort()

        answerList.push({'q': mixKey, 'r': JSON.stringify(currentAnswer)})
      }
    } else {
      //普通题
      let currentAnswer = item.answer
      currentAnswer = currentAnswer.sort()
      answerList.push({'q': key, 'r': JSON.stringify(currentAnswer)})
    }

  }

  return ExamApi.submitExamPlan(examId, subjectId, answerList)

}
//获取考试剩余时间，单位为秒
const getOverTime = (startTime, examDuration) => {
  let endTime = startTime + (examDuration * 60 * 1000)
  let now = moment().format('x')
  let duration = Math.floor((endTime - now) / 1000)
  return duration
}

//检查考试是否被允许
function checkCanExam(subjectId, examId) {
  return ExamAPI.checkCanExam(subjectId, examId)
}
function checkCanExamPlan(planId, examId) {
  return PlanAPI.getPlanDetail(planId)
}
function checkHasBuyPlan(planInfo) {
  if(planInfo.hasBuyPlan === 1 || planInfo.orderInfo.buyStatus === 1){
    return true
  }else{
    return false
  }
}
//当前时间是否在考试时间
function nowInExamTime (data) {
  let startExamTimeLong = data.startExamTimeLong || 0
  let examTimeLimit = data.examTimeLimit || 0
  let endTime = startExamTimeLong + (examTimeLimit * 60 * 1000)
  let now = moment().format('x')
  return (now >= startExamTimeLong && now <= endTime)
}

//缓存考试答案
function saveExamAnswerCache (examData, answerData) {
  return db.saveExamAnswer({"examId": examData.id, "startExamTimeLong": examData.startExamTimeLong, "examTimeLimit": examData.examTimeLimit, "answerData": answerData})
}

//获取缓存的考试答案
function getExamAnswerCache () {
  return db.getExamAnswer()
}

//清除缓存的考试答案
function removeExamAnswerCache () {
  return db.removeExamAnswer()
}


export default {
  getExamDetail,
  getOverTime,
  submitExam,
  submitExamPlan,
  checkCanExam,
  checkCanExamPlan,
  checkHasBuyPlan,
  getExamAnalyze,
  getErrorExamDetail,
  saveExamAnswerCache,
  getExamAnswerCache,
  removeExamAnswerCache,
  nowInExamTime
}
